package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserDecorationOrigin;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserDecorationOriginDaoImpl extends TempDataObjectDao<UserDecorationOrigin> {

    public Map<Integer, UserDecorationOrigin> getUserDecorationOriginMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_decoration_origin where userId=?";
        Map<Integer, UserDecorationOrigin> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserDecorationOrigin data = parse(rs);
                dataMap.put(data.getDecorationId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserDecorationOrigin data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_decoration_origin(userId,decorationId,serverIds,activityId) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDecorationId());
            call.setString(i++, data.getServerIds());
            call.setInt(i++, data.getActivityId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserDecorationOrigin data) {
        PreparedStatement call = null;
        String sql = "update t_u_decoration_origin set serverIds=?,activityId=? where userId=? and decorationId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getServerIds());
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDecorationId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserDecorationOrigin parseData(ResultSet rs) throws SQLException {
        UserDecorationOrigin data = new UserDecorationOrigin();
        data.setUserId(rs.getLong("userId"));
        data.setDecorationId(rs.getInt("decorationId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setServerIds(rs.getString("serverIds"));
        return data;
    }
}
